package com.example.hybridmobiledemo.module.login;

import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.hybridmobiledemo.MainActivity;
import com.example.hybridmobiledemo.R;
import com.example.hybridmobiledemo.view.GuideViewPagerAdapter;

import java.util.ArrayList;
import java.util.List;

public class GuideActivity extends AppCompatActivity  implements View.OnClickListener {
    private ViewPager vp;
    private GuideViewPagerAdapter adapter;
    private List<View> views;
    private Button startBtn;
    private LinearLayout ll;

    // 引导页图片资源
    private static final int[] pics = { R.layout.guid_view1, R.layout.guid_view2,
            R.layout.guid_view3, R.layout.guid_view4, R.layout.guid_view5 };


    // 记录当前选中位置
    private int currentIndex;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_guide);

        views = new ArrayList<View>();

        // 初始化引导页视图列表
        for (int i = 0; i < pics.length; i++) {
            View view = LayoutInflater.from(this).inflate(pics[i], null);
            if (i == pics.length - 1) {
                startBtn = (Button) view.findViewById(R.id.btnToLogin);
                startBtn.setTag("enter");
                startBtn.setOnClickListener(this);
            }

            views.add(view);

        }
        ll = (LinearLayout) findViewById(R.id.ll);
        vp = (ViewPager) findViewById(R.id.vp_guide);
        // 初始化adapter
        adapter = new GuideViewPagerAdapter(views);
        vp.setAdapter(adapter);
        vp.setOnPageChangeListener(new PageChangeListener());

        initDots();
    }

    private void initDots() {

        // 循环取得小点图片
        View pointView;
        for (int i = 0; i < pics.length; i++) {
            // 得到一个LinearLayout下面的每一个子元素
            pointView = new View(GuideActivity.this);
            pointView.setBackgroundResource(R.drawable.radiobutton_off_background);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(25, 25);
            if (i != 0) {
                layoutParams.leftMargin = 10;
            }
            ll.addView(pointView, layoutParams);
        }
        ll.getChildAt(0).setBackgroundResource(R.drawable.radiobutton_on_background);
        currentIndex = 0;
    }

    /**
     * 设置当前view
     *
     * @param position
     */
    private void setCurView(int position) {
        if (position < 0 || position >= pics.length) {
            return;
        }
        vp.setCurrentItem(position);
    }


    @Override
    public void onClick(View view) {
        if (view.getTag().equals("enter")) {
            enterMainActivity();
            return;
        }

        int position = (Integer) view.getTag();
        setCurView(position);
    }

    private void enterMainActivity() {
        Intent intent = new Intent(GuideActivity.this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    private class PageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int position) {

        }

        @Override
        public void onPageScrolled(int position, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {
            // 设置底部小点选中状态
            Log.i("TAG", "" + position);
            int newPosition = position % 5 + 4;
            ll.getChildAt(currentIndex).setBackgroundResource(R.drawable.radiobutton_off_background);
            ll.getChildAt(newPosition).setBackgroundResource(R.drawable.radiobutton_on_background);
            currentIndex = newPosition;
        }

    }
}